/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.session;

import com.thienloc.omap.entity.TinhThanh;
import java.util.List;
import org.slf4j.LoggerFactory;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author GiangLT
 */
@Stateless
public class TinhThanhFacade extends AbstractFacade<TinhThanh> {

    @PersistenceContext(unitName = "omapPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public TinhThanhFacade() {
        super(TinhThanh.class);
    }

    public TinhThanh findByMaTinh(String maTinh) throws Exception {
        try {
            TypedQuery<TinhThanh> q = em.createNamedQuery("TinhThanh.findByMaTinh", TinhThanh.class);
            q.setParameter("maTinh", maTinh);
            TinhThanh result = q.getSingleResult();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(TinhThanhFacade.class.getName()).error("", ex);
            return null;
        }
    }

    public TinhThanh findByTenTinh(String tenTinh) throws Exception {
        try {
            TypedQuery<TinhThanh> q = em.createNamedQuery("TinhThanh.findByTenTinh", TinhThanh.class);
            q.setParameter("tenTinh", tenTinh);
            TinhThanh result = q.getSingleResult();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(TinhThanhFacade.class.getName()).error("", ex);
            return null;
        }
    }

    public List<String> findMaTinhByListVung(List<String> listVungMien) throws Exception {
        try {
            TypedQuery<String> q = em.createNamedQuery("TinhThanh.findMaTinhByListVung", String.class);
            q.setParameter("listVungMien", listVungMien);
            List<String> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(TinhThanhFacade.class.getName()).error("", ex);
            return null;
        }
    }
    
    public List<String> findAllMaTinh() throws Exception {
        try {
            TypedQuery<String> q = em.createNamedQuery("TinhThanh.findAllMaTinh", String.class);
            return q.getResultList();
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(TinhThanhFacade.class.getName()).error("", ex);
            return null;
        }
    }
}
